/*
 * @Author: 谢小康 2170673706@qq.com
 * @Date: 2023-11-23 19:17:14
 * @LastEditors: 谢小康 2170673706@qq.com
 * @LastEditTime: 2024-02-06 14:08:53
 * @FilePath: \my_bolg_admin\my_admin\src\router\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { createRouter, createWebHashHistory } from "vue-router"
import routes from "./routers"
import { Userstore } from "../store/User.js"
import { usePremission } from '../store/premission.js'

const router = createRouter({
    // 4. 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
    history: createWebHashHistory('/admin/'),/*  */
    routes, // `routes: routes` 的缩写
})

router.beforeEach(async (to, from, next) => {
    let Premissionstore = usePremission()
    let userstore = Userstore();
    if (to.path === '/login' || to.path === '/register') {
        if (localStorage.getItem('access_token') && userstore.isLogin) {
            return next({ path: '/home' })
        } else {
            return next()
        }
    }
    if(localStorage.getItem("islogin")){
        next()
    }else{
        next({path:"/login"})
    }
    next()


})

export default router